package org.ttitfly.ttcore.web.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.ttitfly.ttcore.entity.system.Dictionary;
import org.ttitfly.ttcore.service.system.DictionaryManager;
import org.ttitfly.ttcore.util.SimpleMapEntry;
import org.ttitfly.ttcore.web.CrudActionSupport;

/**
 * 用户管理Action.
 * 
 * 使用Struts2 convention-plugin annotation定义Action参数.
 * 演示带分页的管理界面.
 * 
 * @author ttitfly
 */

public class DictionaryAction extends CrudActionSupport<Dictionary> {

	private static final long serialVersionUID = 8683878162525847072L;
	
	@Autowired
	private DictionaryManager dictionaryManager;
	

	//-- 页面属性 --//
	private Long id;
	private Dictionary entity;
	private Page<Dictionary> page = new Page<Dictionary>(5);//每页5条记录
	
	List<Dictionary> result = new ArrayList<Dictionary>();

	//-- ModelDriven 与 Preparable函数 --//
	public void setId(Long id) {
		this.id = id;
	}

	public Dictionary getModel() {
		return entity;
	}
	
	public String dictionary() throws Exception{
		return SUCCESS;
	}
	
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		String keyword = (String) request.getParameter("keyword");
		Dictionary dic = dictionaryManager.findByProperty("keyword", keyword);
		if(dic.getId() != null){
			result = dictionaryManager.findBy("parentId", dic.getId(),"orderNum",true);
		}
		return SUCCESS;
	}
	public List<SimpleMapEntry> getDataList() {
        List<SimpleMapEntry> list = new ArrayList<SimpleMapEntry>();
        for (Dictionary dic : result) {
            list.add(new SimpleMapEntry(String.valueOf(dic.getId()), dic.getTitle()));
        }
        return list;
    }
    
    public int getTotalCount() {
        return result.size();
    }
    
	public List<Dictionary> getResult() {
		return result;
	}

	public void setResult(List<Dictionary> result) {
		this.result = result;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = dictionaryManager.getDictionary(id);
		} else {
			entity = new Dictionary();
		}
	}

	public String login() throws Exception{
		return SUCCESS;
	}
	
	public String first() throws Exception{
		return SUCCESS;
	}
	
	public String initNode(){
		String strParentId = (String)request.getParameter("id");
		if(StringUtils.isBlank(strParentId)){
			strParentId = "0";
		}
		List<Dictionary> list = dictionaryManager.findBy("parentId", Long.parseLong(strParentId),"id",true);
		request.setAttribute("list", list);
		return SUCCESS;
	}
	/**
	 * 删除节点
	 * @return
	 */
	public String dictionaryRemove(){
		dictionaryManager.remove(id);
		return null;
	}
	/**
	 * 修改标题
	 * @return
	 */
	public String ajaxUpdateTitle() throws Exception{
		try{
			String newTitle = request.getParameter("newTitle");
			Dictionary obj = dictionaryManager.getDictionary(id);
			obj.setTitle(newTitle);
			dictionaryManager.saveOrUpdate(obj);
			printJson("{success:true}");
		}catch(Exception e){
			printJson("{success:false}");
		}
		return null;
	}
	/**
	 * 保存节点
	 * @return
	 */
	public String dictionarySave(){
		Dictionary obj = null;
		String id = request.getParameter("id");
		String orderNum = request.getParameter("orderNum");
		String parentId = request.getParameter("parentId");
		String title = request.getParameter("title");
		String keyword = request.getParameter("keyword");
		Integer leaf = 1;
		if(parentId == null || "0".equals(parentId)){
			leaf = 0;
		}
		
		if(StringUtils.isNotBlank(id)){
			obj = dictionaryManager.getDictionary(Long.parseLong(id));
			if(obj == null){
				return ERROR;
			}
		}else{
			obj = new Dictionary();
			obj.setLeaf(leaf);
			obj.setParentId(new Long(parentId));
//			obj.setKeyword(keyword);
		}
		obj.setOrderNum(Integer.parseInt(orderNum));
		obj.setTitle(title);
		obj.setKeyword(keyword);
		dictionaryManager.saveOrUpdate(obj);
		return SUCCESS;	
	}
	
	/**
	 * 进入节点创建或者编辑页面
	 * @return
	 */
	public String dictionaryEdit(){
		String parentId = request.getParameter("parentId");
//		String leaf = request.getParameter("leaf");
		String orderNum = request.getParameter("orderNum");
		String idStr = request.getParameter("id");
		Dictionary dictionary = new Dictionary();
		Integer leaf = 1;
		if(parentId == null || "0".equals(parentId)){
			leaf = 0;
		}
		if(null != idStr){
			dictionary = dictionaryManager.getDictionary(Long.parseLong(idStr));
		}else{
			dictionary.setParentId(new Long(parentId));
			dictionary.setLeaf(leaf);
			dictionary.setOrderNum(new Integer(orderNum));
		}
		request.setAttribute("entity", dictionary);
		return SUCCESS;
		
	}
	

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
}
